#pragma once

#define FTPD_APPNAME	"ftpd"
#define FTPD_VERSION	"20130712"

class Ctftpd
{
public:
	Ctftpd();
	void Run();
private:	
	int Init();

private:
	void AddClient(int fd);
	void CloseClient();
	BOOL CheckPackHeader(PACK_REQUEST* pRequest);	

public:
	bool SetLastPath(const char* szDirectoryName);
	void SetPort(WORD wPort);

private:
	void CmdEntry(PACK_REQUEST* pRequest);
	int ProcessCmdList(PACK_REQUEST* pRequest);
	int ProcessCmdGet(PACK_REQUEST* pRequest);
	int ProcessCmdPut(PACK_REQUEST* pRequest);
	int ProcessCmdRcd(PACK_REQUEST* pRequest);
	int ProcessCmdExec(PACK_REQUEST* pRequest);

private:
	inline int Send(void* buf,DWORD dwDataLen);	
	inline int Recv(void* pBuffer,DWORD dwLen,DWORD dwTimeout=6000);

public:
	void SendList();
	bool RecvRemoteFile(FILE_INFO* pFileInfo,bool bTest=false);
	bool SendLocalFile(const char* pszFileName, DWORD dwFileLength);
	bool SendRespsone(DWORD dwReqId,int nErrCode,void* pData=NULL,DWORD dwDataLen=0);
	bool GetRequestData(PACK_REQUEST* pRequest,void* pData=NULL,DWORD dwDataLen=0);
	

private:
	WORD	m_wPort;
	char	m_szLastPath[PATH_MAX];
	int		m_fd_last;
	int		m_fd_listen;

private:
	fd_set all_set;
};